package com.dorm.cj.dr;

import com.lmax.disruptor.EventFactory;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.YieldingWaitStrategy;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

public class MainTest {
    public static void main(String[] args) {

        EventFactory<MyEvent> eventFactory = new MyEventFactory();

        ExecutorService executor = Executors.newSingleThreadExecutor();

        Disruptor<MyEvent> disruptor = new Disruptor<MyEvent>(eventFactory, 1024, executor, ProducerType.SINGLE, new YieldingWaitStrategy());

        MyEventHandler myEventHandler = new MyEventHandler();
        disruptor.handleEventsWith(myEventHandler);
        disruptor.start();

        RingBuffer<MyEvent> ringBuffer = disruptor.getRingBuffer();
        long nextSequence = ringBuffer.next();
        try {
            MyEvent myEvent = ringBuffer.get(nextSequence);
            myEvent.setData("test for string");
        } finally {
            ringBuffer.publish(nextSequence);
        }

        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
